package com.dofast.bt.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.dofast.bt.R;

import java.util.ArrayList;

/**
 * Created by user on 2016/3/15.
 */
public class HistoryEdit extends EditText{
    private Context mContext;
    private PopupWindow popupWindow;
    private LinearLayout linearLayout;
    public HistoryEdit(Context context) {
        super(context);
        mContext=context;
        initView();
    }
    public HistoryEdit(Context context, AttributeSet attrs) {
        super(context,attrs);
        mContext=context;
        initView();
    }

    public HistoryEdit(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context,attrs,defStyleAttr);
        mContext=context;
        initView();
    }
    /*
    初始化一个LinearLayout布局 用于显示在popupwindow上
     */
    private void initView(){
        linearLayout=new LinearLayout(mContext);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        linearLayout.setOrientation(LinearLayout.VERTICAL);
    }
    /*
    设置历史数据的方法
     */
    @TargetApi(Build.VERSION_CODES.CUPCAKE)
    public void setData(ArrayList<String>  strings){
        if (strings==null||strings.size()==0){   //如果历史数据集合为null，或者不包含历史数据，则移除linerlayout中的所有布局，并且隐藏popupwindow
            linearLayout.removeAllViews();
            if (popupWindow!=null){
                popupWindow.dismiss();
            }
            return;
        }
        for (int i=0;i<strings.size();i++){   //拉取集合中的历史数据 有几个历史数据 就创建几个textview
            final TextView textView=new TextView(mContext);
            textView.setText(strings.get(i));
            textView.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
            textView.setPadding(10, 10, 10, 10);
            textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.ic_launcher, 0);
            textView.setOnClickListener(new OnClickListener() {  //text点击事件 点击后设置edittext的值
                @Override
                public void onClick(View v) {
                    setText(textView.getText().toString());
                    popupWindow.dismiss();
                }
            });
            linearLayout.addView(textView);  ///添加到linerlayout中
        }
        TextView textView=new TextView(mContext);  //添加消除历史记录的textview
        textView.setText("消除历史记录");
        textView.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        textView.setPadding(10, 10, 10, 10);
        textView.setGravity(Gravity.CENTER_HORIZONTAL);
        textView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {      //设置点击事件
                setData(null);
            }
        });
        linearLayout.addView(textView);
    }
    @TargetApi(Build.VERSION_CODES.CUPCAKE)
    public void showWindow(){
        if (popupWindow!=null&&popupWindow.isShowing()){
            return;                          //如果popupwindow 在显示状态  则不作处理
        }else if (popupWindow!=null&&!popupWindow.isShowing()){
            popupWindow.showAsDropDown(this);  //如果popupwindow 是隐藏状态 则显示出来
            return;
        }
        //否则创建popupwindow 并且显示
        popupWindow=new PopupWindow(linearLayout,getWidth(),LinearLayout.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.quanaplha));
        popupWindow.update();
        popupWindow.setFocusable(false);
        popupWindow.showAsDropDown(this);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        //每次手指按下的时候  调用showWindow();
        if (event.getAction()==MotionEvent.ACTION_DOWN){
            showWindow();
        }
        return super.onTouchEvent(event);
    }
}
